﻿@using Nop.Admin.Models.Logistics
@using Telerik.Web.Mvc
@model LogisticsPlanListModel
@using Telerik.Web.Mvc.UI

@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = @T("Admin.Logistics.Plan.List").Text;
}

<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/subsites/Administration/Content/images/ico-catalog.png")" />
        @T("Admin.Logistics.Plan.List")
    </div>
    @*
    <div class="options">
        <a href="@Url.Action("Create")" class="t-button">@T("Admin.Common.AddNew")</a>
        <a href="@Url.Action("ExportXml")" class="t-button">@T("Admin.Common.ExportToXml")</a>
    </div>
    *@
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.StartDate):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.StartDate)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.EndDate):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => Model.EndDate)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SelectedPlanStatus):
        </td>
        <td class="adminData">
            @{
                var idx = 0;
            }
            @foreach (var planStatus in Model.AvailablePlanStatus)
            {
                var planStatusInt = (int) planStatus;
                <input type="checkbox" name="SelectedPlanStatus" value="@planStatusInt"
                @if (Model.SelectedPlanStatus != null && Model.SelectedPlanStatus.Contains(planStatusInt))
                {
                    <text>checked="checked"</text>
                }
                />@Model.AvailablePlanStatusStrings[idx]
                idx++;
                <br />
            }
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-plans" class="t-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid<LogisticsPlanModel>()
                    .Name("logisticsplan-grid")
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Id);
                        columns.Bound(x => x.StatusString)
                            .Title(T("Admin.Logistics.Common.Fields.Status").Text);
                        columns.Bound(x => x.WarehouseAddress.FormattedAddress)
                            .Title(T("Admin.Logistics.WarehouseAddress").Text);
                        columns.Bound(x => x.CourierIdentifier).Width(300);
                        columns.Bound(x => x.Id)
                            .Width(50)
                            .Centered()
                            .Template(x => Html.ActionLink(T("Admin.Logistics.Common.Route").Text, "RoutePlanning", new { id = x.Id }))
                            .ClientTemplate("<a href=\"RoutePlanning/<#= Id #>\">" + T("Admin.Logistics.Common.Route").Text + "</a>")
                            .Title(T("Admin.Logistics.Common.Route").Text);
                        columns.Bound(x => x.Id)
                            .Width(50)
                            .Centered()
                            .Template(x => Html.ActionLink(T("Admin.Logistics.Plan.PlannedRoute").Text, "PlannedRouteList", new { id = x.Id }))
                            .ClientTemplate("<a href=\"PlannedRouteList/<#= Id #>\">" + T("Admin.Logistics.Plan.PlannedRoute").Text + "</a>")
                            .Title(T("Admin.Logistics.Plan.PlannedRoute").Text);
                        columns.Bound(x => x.Id)
                            .Width(50)
                            .Centered()
                            .Template(x => Html.ActionLink(T("Admin.Common.Edit").Text, "PlanEdit", new { id = x.Id }))
                            .ClientTemplate("<a href=\"PlanEdit/<#= Id #>\">" + T("Admin.Common.Edit").Text + "</a>")
                            .Title(T("Admin.Common.Edit").Text);
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .DataBinding(dataBinding => dataBinding.Ajax().Select("PlanList", "Logistics"))
                    .ClientEvents(events => events.OnDataBinding("onDataBinding"))
                    .EnableCustomBinding(true))
        </td>
    </tr>
</table>

<script type="text/javascript">

    $(document).ready(function () {

        //search button
        $('#search-plans').click(function () {
            var grid = $('#logisticsplan-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
    });

    function onDataBinding(e) {
        var planStatusSelected = [];
        $('[name=SelectedPlanStatus]:checked').each(function () {
            planStatusSelected.push($(this).val());
        });

        var searchModel = {
            StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
            EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
            SelectedPlanStatus: planStatusSelected.join(",")
        };
        e.data = searchModel;
    }
</script>
